package com.jushu.storbox;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.constant.IntentConstant;
import com.jushu.storbox.view.resolve.TargetSearchDisplayResolve;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

/**
 * <font color="green"> how to use this activity like this template:</br></font>
 * Intent intent = new Intent(context, TargetFolderBrowserActivity.class);</br>
 * Bundle bundle = new Bundle();</br> <font color="blue">
 * bundle.putString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER,
 * saveToOwnerId);</br> others....</br> </font> intent.putExtra(<font
 * color="red">IntentConstant.INTENT_SEARCH_TARGET_DATA</font>, bundle);</br>
 * startActivityForResult(intent,
 * IntentConstant.INTENT_REQUEST_SEARCH_TARGET);</br> </br> <font color="green">
 * the result like this template:</br></font> Bundle bundle =
 * intent.getBundleExtra(<font
 * color="red">IntentConstant.INTENT_SEARCH_TARGET_DATA</font>);</br> <font
 * color="blue"> String saveToSourceId =
 * bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_ID);</br> String
 * saveToOwnerId =
 * bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER);</br>
 * others....</br> </font> </br> </br> <font color="blue">others</font> is what
 * you want to translate.
 */
public class SaveTargetFolderBrowserActivity extends BaseTitleActivity {

	TargetSearchDisplayResolve displayResolve = null;
	PullToRefreshListView dataShowView;

	String currentFolderId = "";
	String currentOwnerId = "";

	Intent intent = null;
	Bundle bundle = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_target_folder_browser);
		switchTypeTwo();
		rightOperation.setText("确定");
		dataShowView = (PullToRefreshListView) findViewById(R.id.file_action_content_list);

		intent = getIntent();
		if (intent != null) {
			bundle = intent.getBundleExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA);
		}
		String titleName = "";
		if (bundle != null) {
			currentOwnerId = bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER);
			titleName = bundle.getString(IntentConstant.INTENT_TITLE_NAME);

			if (!StringUtil.isBlank(currentOwnerId)) {

				displayResolve = new TargetSearchDisplayResolve(this, currentOwnerId, titleName) {
					protected void onItemOpend(String folderId, String folderName) {
						currentFolderId = folderId;
						setTitleText(folderName);
					}
				};
				displayResolve.initComponent(dataShowView);
				displayResolve.binddingAdapter();
			}
		}
		if (StringUtil.isBlank(titleName)) {
			currentTitle.setText(R.string.my_cloud_drive);
		} else {
			currentTitle.setText(titleName);
		}
	}

	@Override
	protected void onLeftClicked(View clickView) {
		if (StringUtil.isBlank(currentOwnerId)) {
			if (intent == null) {
				intent = new Intent();
			}
			setResult(IntentConstant.INTENT_RESPONSE_SEARCH_TARGET_NULL, intent);
			finish();
			return;
		}

		if (displayResolve.getHistoryStack().size() > 1) {
			displayResolve.openParentFolder();
		} else {
			if (intent == null) {
				intent = new Intent();
			}
			setResult(IntentConstant.INTENT_RESPONSE_SEARCH_TARGET_NULL, intent);
			finish();
		}
	}

	@Override
	protected void onRightClicked(View clickView) {

		if (StringUtil.isBlank(currentOwnerId)) {

			if (intent == null) {
				intent = new Intent();
			}
			setResult(IntentConstant.INTENT_RESPONSE_SEARCH_TARGET_NULL, intent);
			finish();

		} else {

			if (intent == null) {
				intent = new Intent();
			}
			if (bundle == null) {
				bundle = new Bundle();
			}
			bundle.putString(IntentConstant.INTENT_TARGET_RESOURCE_ID, currentFolderId);
			bundle.putString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER, currentOwnerId);
			intent.putExtra(IntentConstant.INTENT_SEARCH_TARGET_DATA, bundle);
			setResult(IntentConstant.INTENT_RESPONSE_SEARCH_TARGET_VALUE, intent);
			finish();

		}

	}

	public void setTitleText(String title) {
		currentTitle.setText(title);
	}
}
